package model;

import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import util.StringUtil;

import model.beans.CategoryBean;
import model.beans.ClientBean;
import model.beans.ItemBean;
import model.beans.ShoppingCartBean;
import model.beans.ShoppingCartItemBean;
import model.dao.CategoryDAO;
import model.dao.ClientDAO;
import model.dao.ItemDAO;
import model.dao.impl.CategoryDAOImp;
import model.dao.impl.ClientDAOImp;
import model.dao.impl.ItemDAOImp;

public class Model {
	private ClientDAO clientDAO;
	private ItemDAO itemDAO;
	private CategoryDAO categoryDAO;
	private Map<Integer,Integer> poNumberCounter;
	
	public Model(){
		this.clientDAO = new ClientDAOImp();
		this.itemDAO = new ItemDAOImp();
		this.categoryDAO = new CategoryDAOImp();
		this.poNumberCounter = new Hashtable<Integer, Integer>();
	}

	public int getPOCounter(int clientNumber){
		Integer counter = this.poNumberCounter.get(clientNumber);
		if(counter == null){
			//set the first order number
			counter = 0;
		}
		
		counter++;
		return counter;
	}
	
	public void updatePOCounter(int clientNumber){
		int current = 0;
		if(this.poNumberCounter.get(clientNumber) == null){
			current = 0;
		}
		else{
			current = this.poNumberCounter.get(clientNumber);				
		}
		current++;
		this.poNumberCounter.put(clientNumber,  current);
	}
	
	public List<CategoryBean> getCategoryList(){
		return categoryDAO.getCategoryList();
	}
	
	//For ItemsByCatalog Page
	public List<ItemBean> getItemListByCatalogID(String selectedCatalog) {
		if(!StringUtil.isEmpty(selectedCatalog)){
			return itemDAO.getItemListByCatalogID(selectedCatalog);
		}
		return null;
	}
	
	//For Login Page
	public ClientBean getClient(String username, String password){	
		return clientDAO.getClient(StringUtil.trim(username), password);
	}
	
	//For Search Page
	public List<ItemBean> getItemListBySearchKeyword(String searchKeyword) {
		List<ItemBean> list = itemDAO.getItemListByKeyword(searchKeyword);
		return list;
	}	
	
	// For Item Detail Page
	public ItemBean getItemByNumber(String number){
		return itemDAO.getItemByNumber(number);
	}

	public void createPO(String filename, ClientBean user, ShoppingCartBean cart, int orderId) {		
		ShoppingCartBean myCart = cart;	
		try	{
			String orderDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			PrintStream ps = new PrintStream(new FileOutputStream(filename));
			ps.println("<?xml version=\"1.0\"?>");
			ps.println("<?xml-stylesheet type=\"text/xsl\" href=\"../po.xsl\"?>");		
			ps.println("<order id=\"" + orderId + "\"" + " submitted=\"" + orderDate + "\""  + ">");
				ps.println("<customer account=\"" + user.getNumber() + "\">");
					ps.println("<name>" + user.getName() + "</name>");
				ps.println("</customer>");
				
				ps.println("<items>");			
					for (ShoppingCartItemBean item : myCart.getItemList())
					{				
						ps.println("<item number=\"" + item.getItem().getNumber() +"\">");
							ps.println("<name>" + item.getItem().getName() + "</name>");
							ps.println("<price>" + item.getItem().getPrice() + "</price>");
							ps.println("<quantity>" + item.getQuantity() + "</quantity>");
							ps.println("<extended>" + item.getExtendedPrice() + "</extended>");
						ps.println("</item>");
					}
				ps.println("</items>");
				
				ps.println("<total>" + myCart.getTotal() + "</total>");
				ps.println("<shipping>" + myCart.getShippingFee() + "</shipping>");
				ps.println("<HST>" + myCart.getHST() + "</HST>");
				ps.println("<grandTotal>" + myCart.getGrandTotal() + "</grandTotal>");
			ps.println("</order>");
		} 
		catch (Exception e){
			System.out.println("file writing error!\n" + e);
		}		
	}

	/**
	 * Instantiate a new ShoppingCartBean
	 * @return ShoppingCartBean
	 */
	public ShoppingCartBean createShoppingCart(){
		ShoppingCartBean sc =  new ShoppingCartBean();		
		return sc;
	}	
}
